package com.jcxy.service.impl;

import com.jcxy.mapper.PositionMapper;
import com.jcxy.pojo.Position;
import com.jcxy.service.PositionService;
import com.jcxy.util.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
public class PositionServiceImpl implements PositionService {

    @Autowired
    private PositionMapper positionMapper;

    @Override
    public PageBean queryListByPage(Map dto, Integer page, Integer limit) {

        Integer count = positionMapper.queryAllPositionCount(dto);
        PageBean pageBean = new PageBean(page, limit);
        dto.put("pageIndex",pageBean.getPageIndex());
        dto.put("limit",pageBean.getLimit());
        List<Position> userList = positionMapper.queryPosition(dto);

        pageBean.setResponseData(count,userList);
        return pageBean;
    }

    @Override
    public List<Position> queryPositionByDeptId(Integer dept_id) {

        return positionMapper.queryPositionByDeptId(dept_id);
    }

    @Override
    @Transactional
    public void insertPosition(Map dto) throws Exception {

        Position position = new Position();
        position.setPosition_desc(dto.get("position_desc").toString());
        position.setPosition_level(dto.get("position_level").toString());
        position.setPosition_name(dto.get("position_name").toString());
        //插入职位基本信息
        positionMapper.insertPosition(position);
        //获取职位所属部门id
        String[] deptIds = dto.get("deptIds").toString().split(",");
        for (String deptId : deptIds){
            position.setDept_id(deptId);
            //插入职位-部门关系信息
            positionMapper.insertPositionRelationDept(position);
        }
    }

    @Override
    @Transactional
    public int updatePosition(Map dto) throws Exception {

        //更新职位基本信息
        int count = positionMapper.updatePosition(dto);

        if (count > 1){
            throw new Exception("修改职位信息异常！");
        }else {
            Position position = new Position();
            position.setPosition_id(Integer.valueOf(dto.get("position_id").toString()));
            //删除职位原本属于的部门关系
            positionMapper.deletePositionRelationDept(position.getPosition_id());

            //插入职位与新的部门关系信息
            //获取职位所属部门id
            String[] deptIds = dto.get("deptIds").toString().split(",");
            for (String deptId : deptIds){
                position.setDept_id(deptId);
                //插入职位-部门关系信息
                positionMapper.insertPositionRelationDept(position);
            }
        }
        return count;
    }

    @Override
    @Transactional
    public int deletePositionById(Integer position_id) throws Exception{

        int count = positionMapper.deletePositionById(position_id);
        if (count > 1){
            throw new Exception("删除职位信息异常");
        }else {
            //删除职位原本属于的部门关系
            positionMapper.deletePositionRelationDept(position_id);
        }
        return count;
    }
}
